import {getSecuExpect, getTopRanks} from "@/backend/services/secu-service";
import {getArticlesByKeywords} from "@/backend/services/article-service";
import {format} from "date-fns";
import Link from "next/link";
import styles from './page.module.scss'

type TopRankType = {
    rank_name: string
}

type ArticleType = {
    art_id: string,
    art_title: string,
    art_content: string,
    art_url: string,
    created_at: string
}

export default async function articlesPage({params}: { params: Promise<{ code: string }> }) {
    const {code} = await params;
    const topRanks: TopRankType[] = await getTopRanks() as TopRankType[];
    const secuExpect = await getSecuExpect(code);
    const eps = secuExpect?.expect || [];
    const secuMixStr = secuExpect?.mix_str || "";
    const keywords: string[] = []
    // 需要搜索的关键词
    for (const r of topRanks) {
        const {rank_name} = r;
        if (secuMixStr.toLowerCase().includes(rank_name.toLowerCase())) {
            keywords.push(rank_name)
        }
    }
    for (const e of eps) {
        keywords.push(e)
    }
    const uniqueKeywords = keywords.filter((v, index, selfArr) => index === selfArr.findIndex(n => n === v))
    const articles: ArticleType[] = await getArticlesByKeywords(uniqueKeywords) as ArticleType[]
    const todayStr = format(new Date(), 'MM-dd');
    return <div>
        {articles.map((item, i) => (
            <div
                key={i}
                className={styles.briefRow}
                style={{color: `${format(item.created_at, 'MM-dd HH:mm').includes(todayStr) ? 'cyan' : 'yellow'}`}}
            >
                <p style={{color: `${item.art_url.includes('weixin.qq.com') ? 'red' : null}`}}>
                    <Link href={item.art_url} target="_blank" style={{display: 'block', padding: 0, margin: 0}}>
                        {format(item.created_at, 'MM-dd HH:mm')} | {item.art_title}
                    </Link>
                </p>
                <p>
                    <span style={{color: 'yellow'}}>
                        {uniqueKeywords.filter((el) => item.art_title.includes(el)).join('；')}
                    </span>
                </p>
            </div>
        ))}
    </div>

}